package com.bxmdm.ragdemo.controller;

import com.bxmdm.ragdemo.domain.EventBean;
import com.bxmdm.ragdemo.service.AgentService;
import com.bxmdm.ragdemo.service.DocumentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.document.Document;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/v1")
@Tag(name = "Spring ai demo")
@RequiredArgsConstructor
public class IndexController {

	private final DocumentService documentService;

	private final AgentService agentService;

	@Operation(summary = "上传文档")
	@PostMapping("/doc/upload")
	public ResponseEntity upload(@RequestBody MultipartFile file) {
		documentService.uploadDocument(file);
		return ResponseEntity.ok("success");
	}

	@Operation(summary = "搜索文档")
	@GetMapping("/doc/search")
	public ResponseEntity<List<Document>> searchDoc(@RequestParam String keyword) {
		return ResponseEntity.ok(documentService.search(keyword));
	}


	@Operation(summary = "文档问答")
	@GetMapping("/doc/chat")
	public ResponseEntity docChat(@Parameter(description = "用户问题",example ="SpringAI中的chatModel有哪些功能?") @RequestParam String message) {
		return ResponseEntity.ok(documentService.chat(message));
	}

	@Operation(summary = "工具问答")
	@GetMapping("/tool/chat")
	public ResponseEntity toolChat(@Parameter(description = "用户问题",example ="现在几点了?") @RequestParam String message) {
		return ResponseEntity.ok(agentService.toolChat(message));
	}

	@Operation(summary = "事件抽取")
	@PostMapping("/news/events")
	public ResponseEntity<List<EventBean>> getNewsEventList(
			@Parameter(description = "用户问题",example ="""
     财联社3月19日讯（编辑 夏军雄）土耳其当局周三拘留了该国总统埃尔多安的主要政治对手埃克雷姆·伊马姆奥卢，罪名包括个人腐败和协助恐怖组织。
     此举引发土耳其金融市场震荡，土耳其基准股指周三盘初一度下跌6.9%，并触发熔断机制；土耳其里拉兑美元汇率当日下跌逾10%，创下历史新低；土耳其10年期政府债券收益率大涨约60个基点。
     """)
			@RequestBody String message) {
		return ResponseEntity.ok(agentService.getNewsEventList(message));
	}

}
